﻿#include "FITKAbaqusHDF5AdaptorMaterialOrientation.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Writer.h"
#include "FITK_Interface/FITKInterfaceModel/FITKCoordinateSystem.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaTransform.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialOrientation.h"

namespace IO
{
    QString FITKAbaqusHDF5AdaptorMaterialOrientation::getAdaptorClass()
    {
        return "FITKAbaqusHDF5AdaptorMaterialOrientation";
    }

    bool FITKAbaqusHDF5AdaptorMaterialOrientation::adaptR()
    {
        auto mo = dynamic_cast<Interface::FITKAbaMaterialOrientation*>(_dataObj);
        if (!_reader || !mo || !_h5Group) return false;
        bool isR = true;
        isR &= readMaterialOrientation(mo, *_h5Group);
        return isR;
    }

    bool FITKAbaqusHDF5AdaptorMaterialOrientation::adaptW()
    {
        auto mo = dynamic_cast<Interface::FITKAbaMaterialOrientation*>(_dataObj);
        if (!_writer || !mo || !_h5Group) return false;
        bool isW = true;
        isW &= writeMaterialOrientation(mo, *_h5Group);
        return isW;
    }

    bool FITKAbaqusHDF5AdaptorMaterialOrientation::readMaterialOrientation(Interface::FITKAbaMaterialOrientation * mo, H5::Group & h5Group)
    {
        if (!mo) return false;
        bool isValid = false;
        bool isR = readNDataObject(mo, h5Group);
        //字符转换枚举 
        auto sMaterialOrientationDefTpye = readStrAttribute(h5Group, "MaterialOrientationDefTpye");
        Core::FITKEnumTransfer<Interface::FITKAbaMaterialOrientation::MaterialOrientationDef> fitkTypeTrafer;
        auto eMaterialOrientationDefTpye = fitkTypeTrafer.fromString(QString::fromStdString(sMaterialOrientationDefTpye), isValid);
        if (!isValid) return false;
        mo->setMaterialOrientationDefTpye(eMaterialOrientationDefTpye);

        int modelSetID = readIntAttribute(h5Group, "ModelSetID");
        mo->setModelSetID(modelSetID);
        int csysID = readIntAttribute(h5Group, "CsysID");
        mo->setCsysID(csysID);
        int rotateAxis = readIntAttribute(h5Group, "RotateAxis");
        mo->setRotateAxis(rotateAxis);
        double rotateAngle = readDoubleAttribute(h5Group, "RotateAngle");
        mo->setRotateAngle(rotateAngle);
        bool isEnableAdditionRotationAngle = readBoolAttribute(h5Group, "EnableAdditionRotationAngle");
        mo->enableAdditionRotationAngle(isEnableAdditionRotationAngle);
        //材料堆叠方向
        int stackingDir = readIntAttribute(h5Group, "StackingDir");
        mo->setStackingDir(stackingDir);
        return isR;
    }

    bool FITKAbaqusHDF5AdaptorMaterialOrientation::writeMaterialOrientation(Interface::FITKAbaMaterialOrientation * mo, H5::Group & h5Group)
    {
        if (!mo) return false;
        bool isValid = false;

        std::string groupName = createParentAttribute(mo, h5Group);
        if (groupName.empty())return false;

        auto materialOrientationGroup = h5Group.createGroup(groupName);
        auto eMaterialOrientationDefTpye = mo->getMaterialOrientationDefTpye();
        //枚举转换字符
        Core::FITKEnumTransfer<Interface::FITKAbaMaterialOrientation::MaterialOrientationDef> fitkTypeTrafer;
        auto sMaterialOrientationDefTpye = fitkTypeTrafer.toStrting(eMaterialOrientationDefTpye, isValid);
        if (!isValid) return false;
        writeStrAttribute(materialOrientationGroup, "MaterialOrientationDefTpye", sMaterialOrientationDefTpye.toStdString());

        int modelSetID = mo->getModelSetID();
        writeIntAttribute(materialOrientationGroup, "ModelSetID", &modelSetID);
        int csysID = mo->getCsysID();
        writeIntAttribute(materialOrientationGroup, "CsysID", &csysID);
        int rotateAxis = mo->getRotateAxis();
        writeIntAttribute(materialOrientationGroup, "RotateAxis", &rotateAxis);
        double rotateAngle = mo->getRotateAngle();
        writeDoubleAttribute(materialOrientationGroup, "RotateAngle", &rotateAngle);
        bool isEnableAdditionRotationAngle = mo->isEnableAdditionRotationAngle();
        writeBoolAttribute(materialOrientationGroup, "EnableAdditionRotationAngle", &isEnableAdditionRotationAngle);
        //材料堆叠方向
        int stackingDir = mo->getStackingDir();
        writeIntAttribute(materialOrientationGroup, "StackingDir", &stackingDir);
        return writeNDataObject(mo, materialOrientationGroup);
    }
}


